import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread("E:\opencv\opencv\sources\samples\data\mili.jpg")
#转换为灰度图
grayImg = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#高斯滤波
blurImg = cv2.GaussianBlur(grayImg,(5,5),0)
#计算直方图
hist = cv2.calcHist([grayImg],[0],None,[256],[0.0,255.0])
#阈值分割黑白二值普通分割
ret1,th1 = cv2.threshold(blurImg,127,255,cv2.THRESH_BINARY)
#大津算法分割
ret2,th2 = cv2.threshold(blurImg,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#分区阈值分割
th3 = cv2.adaptiveThreshold(blurImg,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)

def fill_image(image):
    copyImg = image.copy()
    h,w = image.shape[:2]
    mask = np.zeros([h+2,w+2],np.uint8)
    cv2.floodFill(copyImg,mask,(0,5),(0,255,255),(100,100,0),(100,100,0),cv2.FLOODFILL_FIXED_RANGE)
    cv2.imshow('fillImg',copyImg)
#fill_image(image)
print(ret1,ret2)
plt.plot(hist)
plt.show()
cv2.imshow('image',image)
cv2.imshow('th1',th1)
cv2.imshow('th2',th2)
cv2.imshow('th3',th3)

cv2.waitKey(0)